package com.totoro.service;

import java.util.List;

import org.hibernate.Criteria;
import com.totoro.dao.BaseHibernateDao;
import com.totoro.util.ConditionQuery;
import com.totoro.util.OrderBy;
import com.totoro.util.Page;

public abstract class BaseService<T extends java.io.Serializable, PK extends java.io.Serializable> {
	
	protected BaseHibernateDao<T, PK> baseDao;

	public abstract void setBaseDao(BaseHibernateDao<T, PK> baseDao);

	
	public T save(T model) {
		baseDao.save(model);
		return model;
	}

	public void merge(T model) {
		baseDao.merge(model);
	}

	public void saveOrUpdate(T model) {
		baseDao.saveOrUpdate(model);
	}

	public void update(T model) {
		baseDao.update(model);
	}

	public void delete(PK id) {
		baseDao.delete(id);
	}

	public void deleteObject(T model) {
		baseDao.deleteObject(model);
	}

	public T get(PK id) {
		return baseDao.get(id);
	}

	public int countAll() {
		Criteria criteria = baseDao.createCriteria();
		return baseDao.countAll(criteria);
	}
	
	public List<T> list() {
		return baseDao.list();
	}
	
	public List<T> list(String propertyName, Object value) {
		return baseDao.list(propertyName, value);
	}
	
	public List<T> list(String propertyName, String value){
		return baseDao.list(propertyName, value);
	}
	
	public T uniqueResult(String propertyName,Object value){
		return (T) baseDao.uniqueResult(propertyName, value);
	}
	
	public List<T> findPage(int pageNo, int pageSize) {
		return baseDao.findPage(baseDao.createCriteria(), pageNo, pageSize);
	}

	/*
	 * 分页查询Criteria
	 * 
	 * @param
	 * 
	 * @return
	 */
	public Page<T> pagedQuery(int pageNo, int pageSize) {
		return baseDao.pagedQuery(baseDao.createCriteria(), pageNo, pageSize);
	}

	/*
	 * 分页查询Criteria
	 * 
	 * @param
	 * 
	 * @return
	 */
	public Page<T> pagedQuery(ConditionQuery conditionQuery, OrderBy orderBy,
			int pageNo, int pageSize) {
		return baseDao.pagedQuery(conditionQuery, orderBy, pageNo, pageSize);
	}

	
}
